import {
	ICtrlDataAction,
	ICtrlEvent,
	IParam,
	IDataViewCtrlModel,
	IDataViewCtrlController,
	IDataViewCtrlStore,
	IDataViewCtrlAbility,
	DataViewCtrlActionType,
	IDataViewCtrlControllerParams,
} from '@/core';
import { QXEvent } from 'qx-util';
import { PropType } from 'vue';
import {
	getCtrlClassNames,
	useCtrlAction,
	handleCtrlAction,
	handleCtrlDestroy,
	handleCtrlInit,
	useCtrlBasic,
	useCtrlControllerParams,
	useCtrlProps,
} from '../use-ctrl';

/**
 * 卡片视图输入参数
 *
 * @export
 * @return {*}
 */
export function useDataViewProps() {
	return {
		...useCtrlProps(),
		rowActiveMode: {
			type: Number as PropType<0 | 1 | 2>,
			default: 2,
		},
		openView: Function,
		newView: Function,
		actions: Object as PropType<ICtrlDataAction>,
		selectFirstDefault: Boolean,
	};
}

/**
 * 卡片视图基础参数
 *
 * @export
 * @param {IDataViewModel} model
 * @param {IParam} props
 * @return {*}
 */
export function useDataViewBasic(model: IDataViewCtrlModel, props: IParam) {
	const getClassNames = (props: IParam) => {
		const names = getCtrlClassNames(model, props);
		return names;
	};
	return {
		...useCtrlBasic(model, props),
		getClassNames,
		handleCtrlInit,
		handleCtrlAction,
		handleCtrlDestroy,
	};
}

/**
 * 卡片视图部件行为
 *
 * @export
 */
export function useDataViewAction(
	controller: IDataViewCtrlController<IDataViewCtrlStore, IDataViewCtrlAbility>
) {
	const handleItemClick = (row: IParam, event: MouseEvent) => {
		controller.handleItemClick(row, event);
	};

	const handleItemDbClick = (row: IParam, event: MouseEvent) => {
		controller.handleItemDbClick(row, event);
	};

	const handleSelectionChange = (selections: IParam[]) => {
		controller.handleSelectionChange(selections);
	};

	const handleLoadMore = () => {
		controller.loadMore();
	};
	return {
		...useCtrlAction(controller),
		handleItemClick,
		handleItemDbClick,
		handleSelectionChange,
		handleLoadMore,
	};
}

/**
 * 卡片视图部件控制器
 *
 * @export
 */
export function useDataViewControllerParams<
	S extends IDataViewCtrlStore,
	M extends IDataViewCtrlModel
>(
	model: M,
	props: any,
	evt: QXEvent<ICtrlEvent<DataViewCtrlActionType, IDataViewCtrlAbility>>,
	otherParams: IParam = {}
): IDataViewCtrlControllerParams<DataViewCtrlActionType, IDataViewCtrlAbility> {
	return {
		...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
		actions: props.actions,
		rowActiveMode: props.rowActiveMode,
		selectFirstDefault: props.selectFirstDefault,
		openView: props.openView,
		newView: props.newView,
	};
}
